package net.sourceforge.peertwitter.core.applications.communication;

import java.util.HashSet;
import java.util.Set;

import net.sourceforge.peertwitter.scamp.messaging.HeartbeatMessage;

import rice.p2p.commonapi.Endpoint;
import rice.p2p.commonapi.NodeHandle;

public class SwarmImpl implements Swarm {

	private boolean enabled;
	private final Endpoint endpoint;
	private Set<NodeHandle> participants;

	public SwarmImpl(Endpoint endpoint) {
		this.endpoint = endpoint;
		this.participants = new HashSet<NodeHandle>();
	}

	@Override
	public boolean isEnabled() {
		return this.enabled;
	}

	@Override
	public void enable() {
		this.enabled = true;
		for (NodeHandle nh: this.participants) {
			endpoint.route(null, new HeartbeatMessage(endpoint.getLocalNodeHandle(), 
					endpoint.getEnvironment().getTimeSource().currentTimeMillis(), this.endpoint.getId()), nh);
		}
	}

	@Override
	public void addParticipant(NodeHandle aParticipant) {
		participants.add(aParticipant);
	}

}
